package cn.edu.ctbu.scoremg.api;

import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Score;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.ScoreService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/api/score")
public class ScoreApiController {
    @Autowired
    private ScoreService scoreService;

    /**
     * 查所有成绩
     * @return
     */
    @GetMapping("/list")
    public R<List<Score>> findAll(){
        List<Score> scores = scoreService.getAll();
        return RUtil.success(scores);
    }
    /**
     * 增
     * @param score
     * @return
     */
    @PostMapping("/add")
    public R<Score> add(Score score) {
        return RUtil.success(scoreService.add(score));
    }

    /**
     * 查，id
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Score> findById(@PathVariable Integer id) {
        return RUtil.success(scoreService.findById(id));
    }

    /**
     * 修改
     * @param score
     * @return
     */
    @PutMapping("/update")
    public R<Score> update(Score score) {
        return RUtil.success(scoreService.update(score));
    }

    /**
     * 删，id
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        scoreService.delete(id);
        return RUtil.success();
    }

    /**
     * 删多个
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteId/{ids}")
    public R deleteByIds(@PathVariable String ids) {
        try {
            List<Integer> scoreIds = Stream.of(ids.split(","))
                    .map(Integer::parseInt).collect(Collectors.toList());
            scoreService.deleteByIds(scoreIds);
        } catch (Exception e) {
            return RUtil.error(1, e.getMessage());
        }
        return RUtil.success();
    }

    /**
     * 分页
     * @param qObj
     * @return
     */
    @PostMapping("/getbypage")
    public R<Page<Score>> getByPage(@RequestBody QueryObj<Score> qObj) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Integer pageIndex =0;
        Integer pageSize =10;
        if (qObj==null){
            Pageable pageable= PageRequest.of(pageIndex,pageSize,sort);
            Page<Score> scores = scoreService.getAll(pageable);
            return RUtil.success(scores.getContent(),scores.getTotalElements());
        }else {
            pageIndex=qObj.getPage()-1;
            pageSize=qObj.getLimit();
            if (qObj.getData() instanceof Score){
                Score score = (Score) qObj.getData();
                Pageable pageable= PageRequest.of(pageIndex,pageSize,sort);
                ExampleMatcher matcher=ExampleMatcher.matching()
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();
                Example<Score> example=Example.of(score,matcher);
                Page<Score> scorePage = scoreService.getAll(example,pageable);
                return RUtil.success(scorePage.getContent(),scorePage.getTotalElements());
            }else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}
